﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using QWeiBo.ViewModel;
using TencentWeiboSDK.Services;
using Microsoft.Phone.Tasks;
using System.Windows.Media.Imaging;
using TencentWeiboSDK.Services.Util;
using System.Threading;

namespace QWeiBoN.ViewModule
{
    public class MainPostNewModel : BaseViewModel
    {
        /// <summary>
        /// 实例化微博相关 API 服务.
        /// </summary>
        private TService service = new TService();
        private PhotoChooserTask photoTask = new PhotoChooserTask();
        private string text = string.Empty;
        private BitmapImage imageSource;

        public MainPostNewModel()
        {
            photoTask.Completed +=new EventHandler<PhotoResult>(photoTask_Completed);
        }

        /// <summary>
        /// 分享微博
        /// </summary>
        /// <param name="action">回调委托.</param>
        public void Post(Action action)
        {
            //若用户选择了图片，则实例化 UploadPic 对象，用于上传图片
            //注意：必须在UI线程实例化该对象！
            UploadPic pic = (null != imageSource) ? new UploadPic(imageSource) : null;

            // 为了增加用户体验，使用异步操作
            new Thread(() =>
            {
                Action<Callback<Object>> action1 = new Action<Callback<object>>((callBack) =>
                {
                    Deployment.Current.Dispatcher.BeginInvoke(() =>
                    {
                        if (null == callBack.InnerException)
                        {
                            MessageBox.Show("微博发送成功");
                        }
                        else
                        {
                            MessageBox.Show(callBack.InnerException.Message);
                        }
                    });

                    if (null != action)
                    {
                        ImageSource = null;
                        Text = string.Empty;
                        action();
                    }
                });

                if (null == imageSource)
                {
                    // 发送不带图片的微博
                    service.Add(new ServiceArgument() { Content = Text }, action1);
                }
                else
                {
                    // 发送带图片的微博
                    service.AddPic(new ServiceArgument() { Content = Text, Pic = pic }, action1);
                }

            }).Start();
        }

        /// <summary>
        /// 获取或设置当前分享微博的内容.
        /// </summary>
        public string Text
        {
            get {
                return text;
            }
            set {
                if (value != text)
                {
                    text = value;
                    NotifyPropertyChanged("Text");
                }
            }
        }

        /// <summary>
        /// 获取或设置当前分享微博的图片.
        /// </summary>
        public BitmapImage ImageSource
        {
            get {
                return imageSource;
            }
            set {
                if (value != imageSource)
                {
                    imageSource = value;
                    NotifyPropertyChanged("ImageSource");
                }
            }
        }

        /// <summary>
        /// 选择图片.
        /// </summary>
        public void ChoosePic()
        {
            photoTask.Show();
        }

        /// <summary>
        /// 完成图片选择后的处理方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void photoTask_Completed(object sender, PhotoResult e)
        {
            if (null != e.ChosenPhoto)
            {
                if (null == imageSource)
                {
                    imageSource = new BitmapImage();
                }
                imageSource.SetSource(e.ChosenPhoto);
                NotifyPropertyChanged("ImageSource");
            }
        }

    }
}
